package com.reggie.background.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.background.common.R;
import com.reggie.background.entity.Category;
import com.reggie.background.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品分类或新增套餐分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        if (category==null) {
            return R.error("服务器没有接受到任何参数");
        }
        boolean save = categoryService.save(category);
        if (save) {
            if (category.getType()==1){
                return R.success("菜品分类添加成功！");
            }else {
                return R.success("套餐分类添加成功！");
            }
        }
        else {
            if (category.getType()==0){
                return R.error("菜品分类添加失败！");
            }else {
                return R.error("套餐分类添加失败！");
            }
        }


    }

    /**
     * 分类的分页列表
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<Category>> getPage(int page,int pageSize){
        Page<Category> page1=new Page<Category>(page,pageSize);
        LambdaQueryWrapper<Category> wq=new LambdaQueryWrapper<Category>();
        wq.orderByAsc(Category::getSort);
        categoryService.page(page1,wq);
        return R.success(page1);
    }

    /**
     * 修改菜品分类或套餐分类
     * @param category
     * @return
     */
    @PutMapping
    public R<String> categoryUpdate(@RequestBody Category category){
        if (category==null) {
            return R.error("服务器没有接受到任何参数");
        }
        boolean b = categoryService.updateById(category);
        if (b){
            return R.success("修改成功!");
        }else {
            return R.error("修改失败!");
        }

    }

    /**
     * 根据id删除菜品分类
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> categoryDelete(Long id){
        boolean removeCategory = categoryService.removeCategory(id);
        if (removeCategory) {
            return R.success("删除成功!");
        }else {
            return R.error("删除失败!");
        }
    }

    /**
     * 根据type查询菜品分类或套餐分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Integer type){
        LambdaQueryWrapper<Category> qw=new LambdaQueryWrapper<>();
        qw.eq(type!=null,Category::getType,type);
        List<Category> list = categoryService.list(qw);
        return  R.success(list);
    }
    @GetMapping("/test")
    public R<String[]> test(Integer type){

    return R.success(new String[0]);
    }
}
